package com.teamc.team.service.impl;

import com.teamc.team.entity.Object;
import com.teamc.team.mapper.ObjectMapper;
import com.teamc.team.service.ILinkService;
import com.teamc.team.service.IObjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamc.team.vo.AdminObjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ding
 * @since 2021-06-10
 */
@Service
public class ObjectServiceImpl extends ServiceImpl<ObjectMapper, Object> implements IObjectService {

    @Autowired
    private ILinkService linkService;

    @Autowired
    private ObjectMapper mapper;

    @Override
    public List<AdminObjectVO> adminFindAllObject() {
        return mapper.adminfindAll();
    }

    @Override
    public List<AdminObjectVO> findAllVOByTypeName(String typename) {
        return mapper.adminFindAllByTypeName(typename);
    }

    @Override
    public List<AdminObjectVO> findByObjectID(String id) {
        return mapper.adminFindAllByObjectID(id);
    }

    @Override
    public int change(Object obj) {
        return mapper.updateById(obj);
    }

    @Override
    public int delObject(String id) {
        HashMap<String, java.lang.Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("object_id",id);
        return mapper.deleteByMap(stringObjectHashMap);
    }

    @Override
    public int addObject(Object x) {
        return mapper.insert(x);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean returnBook(Integer linkId) {
        Object object = new Object();
        object.setObjectId(linkService.listAllVOByLinkId(linkId).getObjectId());
        object.setObjectStatus("1");
        this.change(object);
        linkService.returnObject(linkId);
        return true;
    }
}
